<template>
  <el-dialog
    v-loading="loading"
    append-to-body
    :title="title"
    :visible.sync="show"
    width="700px"
    top="3%"
  >
    <el-form label-width="90px">
      <el-form-item label="图片">
        <el-upload
          action="/api/backstage/fileUpload"
          class="avatar-uploader"
          name="file_path"
          :before-upload="beforeAvatarUpload"
          :on-success="handleSuccess"
          :show-file-list="false"
        >
        <div class="img-box" v-if="form.item_path">
          <img :src="form.item_path" width="100%" class="avatar">
        </div>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" style="width: 100px;" @click="saveClicked">保存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { backstage_byteUpload, backstage_fileUpload } from "@/api/login";

export default {
  data() {
    return {
      scale: 1,
      show: false,
      loading: false,
      form: {
          item_path:''
      }
    };
  },
  props: {
    img: {
      type: String,
      default: ""
    },
    title: {
      type: String,
      default: ""
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      let name = file.name.split(".");
      if (
        name[name.length - 1] === "jpg" ||
        name[name.length - 1] === "jpeg" ||
        name[name.length - 1] === "png"
      ) {
        return file;
      } else {
        this.$message.error("图片只能是 jpeg/png 格式!");
        return false;
      }
    },
    handleSuccess(res, file, fileList) {
      if (res.status == 100) {
        this.form.item_path = localStorage.getItem("ossPublicUrl") + "/" + res.data.file_path;
      }
    },
    async saveClicked() {
        if (this.img == "卐") {
          this.$emit("addUeditorImg", this.form.item_path);
        } else {
          this.$emit("update:img", this.form.item_path);
        }
        this.form.item_path = ''
        this.show = false;
    },
    open() {
      this.show = true;
    }
  }
};
</script>

<style lang="scss" scoped>
$border: 1px dashed #d9d9d9;
$picture-size: 120px;
.avatar-uploader {
  .el-upload {
    border: $border;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover {
      border-color: #409eff;
    }
  }
}
.avatar-uploader-icon {
  border: $border;
  font-size: 28px;
  color: #8c939d;
  width: $picture-size;
  height: $picture-size;
  line-height: $picture-size;
  text-align: center;
  border-radius: 5px;
}
.avatar {
  display: block;
  border-radius: 5px;
}
.img-box {
    width: 500px;
}
</style>
